//
//  DDBossInfoService.m
//  DingDongZP
//
//  Created by Jyh on 16/4/20.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBossInfoService.h"
#import "DDIndustry.h"
#import "DDCompanySize.h"
#import "DDCompanyFinance.h"

@implementation DDBossInfoService

- (void)queryBossInfoWithSuccessBlock:(successBlock)successBlock
                            failBlock:(faileBlock)failBlock
{
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:@"employer_query_profile"];
    
    NSMutableDictionary *params = [@{} mutableCopy];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:params success:successBlock failure:failBlock];
}

- (void)queryBossInfoWithRefresh:(BOOL)refresh callback:(void (^)(DDBossInfo *))callback
{
    NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:@"kBossInfo"];
    DDBossInfo *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    if (user && !refresh) {
        callback(user);
    } else {
        [self queryBossInfoWithSuccessBlock:^(id responseObject) {
            
            DDBossInfo *model = [DDBossInfo yy_modelWithJSON:responseObject[@"data"]];
            callback(model);
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
            [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"kBossInfo"];
        } failBlock:^(id responseObject, NSError *error) {
            callback(nil);
        }];
    }
}

- (void)queryBossInterestingCattleWithPage:(NSString *)page
                              SuccessBlock:(successBlock)successBlock
                                 failBlock:(faileBlock)failBlock
{
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:@"query_interesting_cattle"];
    
    NSMutableDictionary *params = [@{} mutableCopy];
    if (page) {
        params[@"page"] = page;
    }
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:params success:successBlock failure:failBlock];
}


- (void)eidtInfoType:(DDBossInfoType)infoType value:(id)value callback:(void (^)(BOOL succ, NSString *err))callback
{
    NSString *apiname = nil;
    NSMutableDictionary *params = [NSMutableDictionary new];
    switch (infoType) {
        case DDBossInfoTypeAvatar: {
            apiname = @"employer_upload_avatar";
            params[@"file"] = value;
            
            /**
             *  @brief 上传图片
             */
            NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
            
            DDNetWorkManager *manager = [DDNetWorkManager shareManager];
            [manager UPLOAD:URLString parameters:nil filesDatas:@{@"file": value} success:^(id responseObject) {
                callback([responseObject[@"code"] boolValue], responseObject[@"msg"]);
            } failure:^(id responseObject, NSError *error) {
                callback(NO, error.domain);
            }];
            
            /// 完成后结束流程
            return;
            
            break;
        }
        case DDBossInfoTypeName: {
            apiname = @"employer_set_name";
            params[@"name"] = value;
            break;
        }
        case DDBossInfoTypeWx: {
            apiname = @"employer_set_weixin";
            params[@"weixin"] = value;
            break;
        }
        case DDBossInfoTypeEmail: {
            apiname = @"employer_set_email";
            params[@"email"] = value;
            break;
        }
        case DDBossInfoTypePosition: {
            apiname = @"employer_set_position";
            params[@"position_id"] = value;
            break;
        }
        case DDBossInfoTypePositionCustom: {
            apiname = @"employer_set_custom_position";
            params[@"position_customized"] = value;
            break;
        }
        case DDBossInfoTypeLight: {
            apiname = @"employer_set_hightlight";
            params[@"tags"] = value;
            break;
        }
        case DDBossInfoTypeIntro: {
            apiname = @"employer_set_team_intro";
            params[@"team_intro"] = value;
            break;
        }
        case DDBossInfoTypeShortName: {
            apiname = @"employer_set_company_shortname";
            params[@"company_shortname"] = value;
            break;
        }
        case DDBossInfoTypeIndustry: {
            apiname = @"employer_set_company_industry";
            params[@"industry_id"] = value;
            break;
        }
        case DDBossInfoTypeCompanySize: {
            apiname = @"employer_set_company_size";
            params[@"size_id"] = value;
            break;
        }
        case DDBossInfoTypeFiniance: {
            apiname = @"employer_set_company_finance";
            params[@"financing_id"] = value;
            break;
        }
        case DDBossInfoTypeFullname: {
            apiname = @"employer_set_company_fullname";
            params[@"company_fullname"] = value;
            break;
        }
        case DDBossInfoTypeWebsite: {
            apiname = @"employer_set_company_website";
            params[@"company_website"] = value;
            break;
        }
        case DDBossInfoTypeAddress: {
            apiname = @"employer_set_company_address";
            params[@"company_address"] = value;
            break;
        }
    }
    
    if (apiname && params.count > 0) {
        NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
        
        DDNetWorkManager *manager = [DDNetWorkManager shareManager];
        
        [manager POST:URLString parameters:params success:^(id responseObject) {
//            NSLog(@"修改个人信息返回数据:%@", responseObject);
            callback([responseObject[@"code"] boolValue], responseObject[@"msg"]);
        } failure:^(id responseObject, NSError *error) {
            callback(NO, error.domain);
        }];
    }
}

- (void)queryHighlights:(void (^)(NSArray *))callback
{
    NSString *apiname = @"employer_query_highlight";
    NSMutableDictionary *params = [NSMutableDictionary new];
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:params success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSArray *datas = responseObject[@"data"];
            NSArray *models = [NSArray yy_modelArrayWithClass:[DDTeamHighlightTag class] json:datas];
            callback(models);
        }
        else {
            callback(nil);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(nil);
    }];
}

- (void)queryEmployerPositions:(void (^)(NSArray *))callback
{
    NSString *apiname = @"employer_query_positions";
    NSMutableDictionary *params = [NSMutableDictionary new];
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:params success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSArray *datas = responseObject[@"data"];
            NSArray *models = [NSArray yy_modelArrayWithClass:[DDBossPosition class] json:datas];
            callback(models);
        }
        else {
            callback(nil);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(nil);
    }];
}

- (void)queryIndustry:(void (^)(NSArray *))callback
{
    NSString *apiname = @"query_industry";
    NSMutableDictionary *params = [NSMutableDictionary new];
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:params success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSArray *datas = responseObject[@"data"];
            NSArray *models = [NSArray yy_modelArrayWithClass:[DDIndustry class] json:datas];
            callback(models);
        }
        else {
            callback(nil);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(nil);
    }];
}

- (void)queryCompanySize:(void (^)(NSArray *))callback
{
    NSString *apiname = @"query_copanysize";
    NSMutableDictionary *params = [NSMutableDictionary new];
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:params success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSArray *datas = responseObject[@"data"];
            NSArray *models = [NSArray yy_modelArrayWithClass:[DDCompanySize class] json:datas];
            callback(models);
        }
        else {
            callback(nil);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(nil);
    }];
}

- (void)queryCompanyFinance:(void (^)(NSArray *companyFinance))callback
{
    NSString *apiname = @"query_company_finance";
    NSMutableDictionary *params = [NSMutableDictionary new];
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:params success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSArray *datas = responseObject[@"data"];
            NSArray *models = [NSArray yy_modelArrayWithClass:[DDCompanyFinance class] json:datas];
            callback(models);
        }
        else {
            callback(nil);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(nil);
    }];
}

- (void)signUpWithCallback:(void (^)(BOOL succ))callback {
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:@"employer_sign_in"];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager POST:URLString parameters:nil success:^(id responseObject) {
//        NSLog(@"修改个人信息返回数据:%@", responseObject);
        callback([responseObject[@"code"] boolValue]);
    } failure:^(id responseObject, NSError *error) {
        callback(NO);
    }];
}

- (void)querySkillRequirementArrayWithLevelID:(NSString *)levelID callback:(void (^)(NSArray *skillArray))callback
{
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:@"query_jobstrenghs_requirments_tags_for_level2jobcategory"];
    
    NSMutableDictionary *params = [@{} mutableCopy];
    if (levelID) {
        params[@"level2_id"] = levelID;
    }
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    
    [manager GET:URLString parameters:params success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSArray *datas = responseObject[@"data"];
            NSArray *models = [NSArray yy_modelArrayWithClass:[DDTeamHighlightTag class] json:datas];
            callback(models);
        }
        else {
            callback(nil);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(nil);
    }];

}

/**
 *  @brief 增加活跃度
 *
 *  @param type     活跃度类型，参见上方宏定义
 *  @param callback 回调方法
 */
- (void)addActivenessWithType:(NSString *)type callback:(void (^)(BOOL succ, id data))callback
{
    if (!type) {
        callback(NO, @"type不能为空");
    }
    NSString *requestUrlString = [[DDAPIGenerate sharedInstance] API:@"employer_add_activeness"];
    
    [[DDNetWorkManager shareManager] POST:requestUrlString parameters:@{@"type": type} success:^(id responseObject) {
        NSInteger code = [responseObject[@"code"] integerValue];
        if (code == 1) {
            NSNumber *newActive = responseObject[@"data"];
            callback(YES, newActive);
        } else {
            NSString *err = responseObject[@"msg"];
            callback(NO, err);
        }
    } failure:^(id responseObject, NSError *error) {
        callback(NO, error.domain);
    }];
}

@end
